package uk.ac.aber.paws.client.idlelogger.languages;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import uk.ac.aber.paws.client.config.ConfigManager;
import uk.ac.aber.paws.client.idlelogger.IdleLogger;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * Languages.java
 * 
 * Used by IdleLogger to provide multi-lingual support to the front end
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Languages {

	private static String selectedLanguage = IdleLogger.getMainConfig().getProperty("app.language.default")+IdleLogger.getMainConfig().getProperty("app.language.identifier");

	/**
	 * Returns the current selected language to the caller
	 * @return The language in the form en.lang (for example)
	 */
	public static String getSelectedLanguage() {
		
		return selectedLanguage;
	}
	
	/**
	 * Sets the current selected language
	 * @param lang The language in the form en.lang (for example)
	 */
	public static void setSelectedLanguage(String lang) {
		
		selectedLanguage = lang;
	}
	
	/**
	 * Gets the list of available languages names and keys from config/languages.cfg
	 * @param path The paths to the cfg file containing this information
	 * @return A hashtable returning the data, e.g. en.lang=English 
	 */
	public static Hashtable<String,String> getLangFileNames(String path) {
		
		Hashtable<String,String> filenames = new Hashtable<String,String>();
		
		ConfigManager langs = new ConfigManager(path,null,true);
		
		Properties langList = langs.getAllProperties();
		
		Enumeration<Object> keys = langList.keys();
		
		while (keys.hasMoreElements()) {
		
			String key = (String)keys.nextElement();
			
			String value = (String)langList.get(key);
			
			if (key.endsWith(IdleLogger.getMainConfig().getProperty("app.language.identifier"))) {
				filenames.put(key,value);
			}
		}
		
		return filenames;
	}
}
